from django.shortcuts import render, redirect

# Create your views here.
from app02 import models
from app02.utils.form import UserModelForm, PrettyModelForm
from app02.utils.pagination import Pagination


def depart_list(request):
    querySet = models.Department.objects.all()
    return render(request,"depart_list.html",{'queryset':querySet})

def depart_add(request):
    """ 添加部门 """
    if request.method == "GET":
        return render(request,"depart_add.html")
    # 获取用户POST提交过来的数据（title输入为空）
    title = request.POST.get("title")
    # 保存到数据库
    models.Department.objects.create(title=title)
    # 重定向回部门列表
    return redirect("/depart/list")

def depart_delete(request):
    """ 删除部门 """
    nid = request.GET.get("nid")
    # 删除
    models.Department.objects.filter(id=nid).delete()
    # 重定向回部门列表
    return redirect("/depart/list")

def depart_edit(request,nid):
    if request.method == "GET":
        # 根据nid，获取他的数据 [obj,]
        row_object = models.Department.objects.filter(id=nid).first();
        return render(request,'depart_edit.html',{"row_object":row_object})
    # 获取用户提交的标题
    title = request.POST.get("title")
    # 根据ID找到数据库中的数据并进行更新
    # models.Department.objects.filter(id=nid).update(title=title,其他=123)
    models.Department.objects.filter(id=nid).update(title=title)
    # 重定向回部门列表
    return redirect("/depart/list")
